package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.dto.AdministrativeOfficeDTO;
import com.hksj.Intelligentmanagement.entity.AdministrativeOfficeEntity;
import com.hksj.Intelligentmanagement.mapper.AdministrativeOfficeMapper;
import com.hksj.Intelligentmanagement.vo.AdministrativeOfficeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName AdministrativeOfficeService
 * @Description TODO
 * @Author @陈章
 * @Date 2023/3/31 17:55
 * @Version 1.0
 */
@Service
public class AdministrativeOfficeService extends ServiceImpl<AdministrativeOfficeMapper, AdministrativeOfficeEntity> {

    @Autowired
    private AdministrativeOfficeMapper administrativeOfficeMapper;

    public void addAdministrativeOffice(AdministrativeOfficeDTO administrativeOfficeDTO) {
        AdministrativeOfficeEntity administrativeOfficeEntity = new AdministrativeOfficeEntity();
        BeanUtils.copyProperties(administrativeOfficeDTO, administrativeOfficeEntity);

        QueryWrapper<AdministrativeOfficeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("administrative_office_card", administrativeOfficeDTO.getAdministrativeOfficeCard());
        AdministrativeOfficeEntity entity = administrativeOfficeMapper.selectOne(wrapper);
        if (entity != null) {
            throw new RuntimeException("科室编号不能重复");
        }
        int insert = administrativeOfficeMapper.insert(administrativeOfficeEntity);
        if (insert <= 0) {
            throw new RuntimeException("新增失败");
        }
    }

    public void dellAdministrativeOffice(String id) {
        int i = administrativeOfficeMapper.deleteById(id);
        if (i <= 0) {
            throw new RuntimeException("删除失败");
        }
    }

    public List<AdministrativeOfficeVo> selectAdministrativeOffice() {
        List<AdministrativeOfficeVo> list=administrativeOfficeMapper.selectAdministrativeOffice();
        return  list;
    }

    public void upAdministrativeOffice(AdministrativeOfficeDTO administrativeOfficeDTO) {
        AdministrativeOfficeEntity administrativeOfficeEntity = new AdministrativeOfficeEntity();
        BeanUtils.copyProperties(administrativeOfficeDTO, administrativeOfficeEntity);
        int i = administrativeOfficeMapper.updateById(administrativeOfficeEntity);
        if (i<=0){
            throw new RuntimeException("更新失败");
        }
    }

    public void upOfficeStatus(String id, String status) {
        int i = administrativeOfficeMapper.upOfficeStatus(id, status);
        if (i<=0){
            throw new RuntimeException("更新失败");
        }
    }
}
